<%@ page import="com.wl.shixun1.mymvc.DoNum" %>
<%@ page import="com.wl.shixun1.mymvc.Num" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%!
    // 根据操作符返回对应的符号
    String getOperatorSymbol(String op) {
        switch (op) {
            case "add": return "+";
            case "sub": return "-";
            case "mul": return "×";
            case "div": return "÷";
            default: return op;
        }
    }
%>
<html>
<head>
    <title>2350300097 万朗</title>
</head>
<body>
<h2>三数计算器</h2>
<form action="" method="post">
    <div>
        <input type="text" name="num1" placeholder="第一个数" value="<%=request.getParameter("num1") == null ? "" : request.getParameter("num1")%>">
        <select name="op1">
            <option value="add" <%= "add".equals(request.getParameter("op1")) ? "selected" : "" %>>+ 加</option>
            <option value="sub" <%= "sub".equals(request.getParameter("op1")) ? "selected" : "" %>>- 减</option>
            <option value="mul" <%= "mul".equals(request.getParameter("op1")) ? "selected" : "" %>>× 乘</option>
            <option value="div" <%= "div".equals(request.getParameter("op1")) ? "selected" : "" %>>÷ 除</option>
        </select>
        <input type="text" name="num2" placeholder="第二个数" value="<%=request.getParameter("num2") == null ? "" : request.getParameter("num2")%>">
        <select name="op2">
            <option value="add" <%= "add".equals(request.getParameter("op2")) ? "selected" : "" %>>+ 加</option>
            <option value="sub" <%= "sub".equals(request.getParameter("op2")) ? "selected" : "" %>>- 减</option>
            <option value="mul" <%= "mul".equals(request.getParameter("op2")) ? "selected" : "" %>>× 乘</option>
            <option value="div" <%= "div".equals(request.getParameter("op2")) ? "selected" : "" %>>÷ 除</option>
        </select>
        <input type="text" name="num3" placeholder="第三个数" value="<%=request.getParameter("num3") == null ? "" : request.getParameter("num3")%>">
        <input type="submit" value="计算">
    </div>
</form>

<%
    String num1 = request.getParameter("num1");
    String num2 = request.getParameter("num2");
    String num3 = request.getParameter("num3");
    String op1 = request.getParameter("op1");
    String op2 = request.getParameter("op2");
    String resultStr = "";
    String expression = "";

    if (num1 != null && !num1.trim().isEmpty() &&
        num2 != null && !num2.trim().isEmpty() &&
        num3 != null && !num3.trim().isEmpty() &&
        op1 != null && op2 != null) {

        try {
            Num n1 = DoNum.fun(num1);
            Num n2 = DoNum.fun(num2);
            Num n3 = DoNum.fun(num3);

            Num result = n1.calculateExpression(n2, n3, op1, op2);

            if (result != null) {
                resultStr = result.draw1();
                expression = num1 + " " + getOperatorSymbol(op1) + " " + num2 + " " + getOperatorSymbol(op2) + " " + num3 + " = ";
            } else {
                resultStr = "计算出错";
            }
        } catch (Exception e) {
            resultStr = "计算出错: " + e.getMessage();
        }
    }
%>

<% if (!resultStr.isEmpty()) { %>
    <div class="expression"><%=expression%><%=resultStr%></div>
<% } %>

</body>
</html>